package com.qiqi.admin.basicdata.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qiqi.basicdata.dto.ColorMaterialDTO;
import com.qiqi.basicdata.entity.ColorMaterialDO;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qiqi.common.entity.PageEntity;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import com.qiqi.basicdata.service.ColorMaterialService;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author QiQiDream
 * @since 2020-11-30
 */
@Api("相关接口")
@RestController
@RequestMapping("/color/material")
public class ColorMaterialController {

    @Resource
    private ColorMaterialService colorMaterialService;

    @ApiOperation(value = "获取(列表)")
    @PostMapping("/list")
    public PageEntity<ColorMaterialDO> getColorMaterialPage(@RequestBody ColorMaterialDTO colorMaterial) {
        LambdaQueryWrapper<ColorMaterialDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(colorMaterial.getCode()),ColorMaterialDO::getCode,colorMaterial.getCode())
                .like(StringUtils.isNotBlank(colorMaterial.getName()),ColorMaterialDO::getName,colorMaterial.getName())
                .eq(StringUtils.isNotBlank(colorMaterial.getType()),ColorMaterialDO::getType,colorMaterial.getType());
        IPage<ColorMaterialDO> iPage = colorMaterialService.page(new Page<>(colorMaterial.getPage(),colorMaterial.getCount()),queryWrapper);
        return new PageEntity<>(iPage.getTotal(),Convert.convert(new TypeReference<List<ColorMaterialDO>>() {}, iPage.getRecords()));
    }

    @ApiOperation(value = "获取(单个)")
    @GetMapping("/{id}")
    public ColorMaterialDO getColorMaterial(@PathVariable Long id) {
        return colorMaterialService.getById(id);
    }

    @ApiOperation(value = "获取列表(下拉框)")
    @GetMapping("all")
    public List<ColorMaterialDO> getList(){
        return colorMaterialService.list();
    }

    @ApiOperation(value = "新增")
    @PostMapping("/add")
    public Boolean saveColorMaterial(@RequestBody ColorMaterialDO colorMaterialDO) {
        return colorMaterialService.saveOrUpdate(colorMaterialDO);
    }

    @ApiOperation(value = "删除(单个))")
    @DeleteMapping("/{id}")
    public Boolean deleteColorMaterialById(@PathVariable Long id) {
        return colorMaterialService.removeById(id);
    }
}
